package com.dependencies.h2database.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * @author zhizhou   2024/8/9 00:30
 */
@Configuration
public class AppConfig {
    
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.h2.Driver");
        //JDBC URL: 可以使用内存数据库（如 jdbc:h2:mem:testdb）或文件数据库（如 jdbc:h2:~/testdb）。
        //dataSource.setUrl("jdbc:h2:mem:testdb");
        dataSource.setUrl("jdbc:h2:~/Users/zhizhou/Downloads/testdb");
        dataSource.setUsername("sa");
        dataSource.setPassword("password");
        return dataSource;
    }
    
    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
